<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Http\Resources\Estate\WaterMeterResource\WaterMeterCollection;
use App\Models\ElectricAlarmPlan;
use App\Models\ElectricChargingScheme;
use App\Models\ElectricCollector;
use App\Models\ElectricCommunity;
use App\Models\ElectricWater;
use App\Services\WaterMeterService;
use Illuminate\Http\Request;

/**
 * 水表管理
 * Class WaterMeterController
 * @package App\Http\Controllers\Estate
 */
class WaterMeterController extends Controller
{
    /**
     * 2021/3/15--14:31
     * @effect:水表列表
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:[index]

     * @param Request $request
     * @param WaterMeterService $water_service
     * @return array
     */
    public function index(Request $request, WaterMeterService $water_service)
    {

        $rs = $water_service->get_list();
        return $rs['status'] ? $this->success(new WaterMeterCollection($rs['data'])) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/15--14:32
     * @param Request $request
     * @param WaterMeterService $water_service
     * @return array
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:[store]
     * @effect:添加水表设备
     */
    public function store(Request $request, WaterMeterService $water_service)

    {
        $water_service->add();
        return $this->success([], __('base.success'));
    }

    /**
     * 2021/3/15--14:31
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:[show]
     * @effect:展示水表数据
     * @param ElectricWater $meter_model
     * @param int $id
     * @return array
     */
    public function show(ElectricWater $meter_model, int $id)
    {
        $info = $meter_model->find($id);
        return $this->success($info);
    }

    /**
     * 2021/3/15--14:33
     * @param WaterMeterService $water_service
     * @return array
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:[update]
     * @effect:更新水表数据.
     */
    public function update(WaterMeterService $water_service)
    {
        $water_service->edit();
        return $this->success([], __('base.success'));
    }

    /**
     * 2021/3/15--14:33
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:destroy
     * @effect:删除水表
     * @param ElectricWater $meter_model
     * @param $id
     * @return array
     */
    public function destroy(ElectricWater $meter_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $meter_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    /**
     * 2021/3/15--14:33
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:get_community
     * @effect:获取区域,采集器，缴费方案，报警方案数组
     * @return array
     */
    public function get_community()
    {
        $community_arr = [];
        $community_db = ElectricCommunity::selectRaw('id value,name label')->where(['status' => 1])->get();
        if ($community_db->count()) {
            $community_arr = $community_db;
        }
        $collector_arr = [];
        $collector_db = ElectricCollector::selectRaw('id value,name label')->where(['status' => 1])->get();
        if ($collector_db->count()) {
            $collector_arr = $collector_db;
        }
        $alarm_arr = [];
        $alarm_db = ElectricAlarmPlan::selectRaw('id value,plan_title label')->where(['status' => 1])->get();
        if ($alarm_db->count()) {
            $alarm_arr = $alarm_db;
        }
        $plan_arr = [];
        $plan_db = ElectricChargingScheme::selectRaw('id value,plan_title label')->where(['status' => 1])->get();
        if ($plan_db->count()) {
            $plan_arr = $plan_db;
        }
        return $this->success(compact("collector_arr","community_arr","alarm_arr","plan_arr"));
    }

    /**
     * @Notes 获取详情
     * @param $communityId
     * @return array
     * @author ypp
     * @date 2021/3/18
     * @time 14:38
     */
    public function waterDetail($id,WaterMeterService $waterMeterService)
    {
        $result = $waterMeterService->getElectricDetail($id);
        return $result['status'] ? $this->success($result['data']) : $this->error('获取失败');
    }
    /**
     * @Notes 获取实时数据页面展示
     * @param $communityId
     * @return array
     * @author ypp
     * @date 2021/3/18
     * @time 14:38
     */
    public function waterTimeData($id,$communityId,$tableAddress,$code,WaterMeterService $waterMeterService)
    {
        $data = $waterMeterService->getTimeData($id,$communityId,$tableAddress,$code);
        return $this->success($data);
    }

    /**
     * @Notes 电表详情数据报表数据
     * @param WaterMeterService $waterMeterService
     * @return array
     * @author ypp
     * @date 2021/3/19
     * @time 15:56
     */
    public function waterDataReport(WaterMeterService $waterMeterService)
    {
        $data = $waterMeterService->getDataReport();
        return $this->success($data);
    }

    /**
     * @Notes 电量图表
     * @param WaterMeterService $waterMeterService
     * @return array
     * @author ypp
     * @date 2021/3/19
     * @time 15:58
     */
    public function waterReport(WaterMeterService $waterMeterService){
        $data = $waterMeterService->getElectricReport();
        return $this->success($data);
    }
}
